package com.example.pixelyear.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.Log
import android.view.View
import com.example.pixelyear.R
import com.example.pixelyear.utils.UIUtils

/*
 * Package    :com.example.pixelyear.view
 * ClassName  :RoundImageView
 * Description:圆角ImageView
 * Data       :2020/10/18 16:43
 */
class RoundImageView @JvmOverloads constructor(
    context: Context,
    attributes: AttributeSet? = null,
    defaultStyle: Int = 0
) :
    View(context, attributes, defaultStyle) {

    private val mPaint by lazy {
        Paint().also {
            it.color = Color.WHITE
            it.flags = Paint.ANTI_ALIAS_FLAG
        }
    }

    private val roundRX = 10f//圆角X半径
    private val roundRY = 10f//圆角Y半径

    private val roundRectF by lazy {
        RectF()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        val w: Int
        val h: Int

        if (widthMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
            w = UIUtils.getDimension(R.dimen.dp_40)
        } else w = widthMeasureSpec

        if (heightMode == MeasureSpec.AT_MOST || heightMode == MeasureSpec.UNSPECIFIED) {
            h = UIUtils.getDimension(R.dimen.dp_40)
        } else h = heightMeasureSpec
        setMeasuredDimension(w, h)
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        //画有标记的日期
        roundRectF.left = 0f
        roundRectF.top = 0f
        roundRectF.right = width.toFloat()
        roundRectF.bottom = height.toFloat()
        canvas?.drawRoundRect(
            roundRectF, roundRX, roundRY, mPaint
        )
    }

    fun setColor(color: Int) {
        mPaint.color = color
        invalidate()
    }


}